﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Security;
using MentorBackEnd.Model;

namespace WebApplication1
{
    public partial class Register : System.Web.UI.Page
    {
        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);

            PasswordLength.ServerValidate += new ServerValidateEventHandler(PasswordLength_ServerValidate);
            CreateUserButton.Click += new EventHandler(CreateUserButton_Click);
        }

        void CreateUserButton_Click(object sender, EventArgs e)
        {
            if (!IsValid) return;

            MemaUser user = new MemaUser();
            user.Email = Email.Text;
            user.Password = Password.Text;
            user.FirstName = FirstName.Text;
            user.LastName = LastName.Text;
            user.UserType = rdoMentor.Checked ? EnumTypes.MemaUserType.Mentor : EnumTypes.MemaUserType.Mentee;
            MentorBackEnd.DA.UserDA.CreateNewUser(user);
            user = MentorBackEnd.DA.UserDA.Authenticate(Email.Text, Password.Text);
            FormsAuthentication.SetAuthCookie(user.MeMaUserID.ToString(), false);
            Response.Redirect("~/Account/Profile.aspx");
        }

        void PasswordLength_ServerValidate(object source, ServerValidateEventArgs args)
        {
            args.IsValid = args.Value.Length >= System.Web.Security.Membership.MinRequiredPasswordLength;
        }
    }
}